package org.jf.tool.fx.jftoolfx.format.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextArea;
import org.jf.tool.fx.jftoolfx.common.BaseController;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author jifeng_zhang
 * @date 2025/9/22 下午1:15
 */
public class JsonFormatController extends BaseController {
    @FXML
    public Spinner<Integer> indentSize;
    @FXML
    private TextArea textArea;
    @FXML
    private TextArea resultArea;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
    }

    @FXML
    private void format() {
        try {
            String jsonString = textArea.getText();
            if (jsonString == null || jsonString.isEmpty()) {
                resultArea.setText("请输入JSON字符串");
                return;
            }
            String jsonFormat = formatJsonWithIndent(jsonString, indentSize.getValue());
            resultArea.setText(jsonFormat);
        } catch (Exception e) {
            resultArea.setText("JSON 格式化失败：" + e.getMessage());
        }
    }

    /**
     * 使用指定空格数格式化JSON
     */
    public static String formatJsonWithIndent(String jsonString, int indentSpaces) {
        try {
            Object jsonObject = MAPPER.readValue(jsonString, Object.class);
            // 启用美化输出
            MAPPER.enable(SerializationFeature.INDENT_OUTPUT);

            if (indentSpaces <= 0) {
                // 压缩模式
                return MAPPER.writer().writeValueAsString(jsonObject);
            }

            // 创建自定义的PrettyPrinter
            DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();

            // 设置缩进, 使用空格
            String indent = " ".repeat(indentSpaces);
            DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(indent, DefaultIndenter.SYS_LF);
            prettyPrinter.indentObjectsWith(indenter);
            prettyPrinter.indentArraysWith(indenter);
            MAPPER.setDefaultPrettyPrinter(prettyPrinter);

            // 使用自定义的美化打印机
            return MAPPER.writeValueAsString(jsonObject);

        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("无效的JSON字符串: " + e.getMessage(), e);
        }
    }

    /**
     * 使用制表符格式化JSON
     */
    public static String formatJsonWithTab(String jsonString) {
        try {
            Object jsonObject = MAPPER.readValue(jsonString, Object.class);

            DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
            DefaultPrettyPrinter.Indenter tabIndenter = new DefaultIndenter("\t", DefaultIndenter.SYS_LF);

            printer.withObjectIndenter(tabIndenter).withArrayIndenter(tabIndenter);

            return MAPPER.writer(printer).writeValueAsString(jsonObject);

        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("无效的JSON字符串: " + e.getMessage(), e);
        }
    }

    @FXML
    private void clear() {
        textArea.setText("");
        resultArea.setText("");
    }

    @FXML
    private void copy(ActionEvent event) {
        copy(resultArea.getText());
    }
}
